import Vue from 'vue'
import axios from 'axios'
import store from '../store'
axios.defaults.baseURL = '/'
axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'

const setProgress = (config, progress) => config.noInterceptor || store.dispatch('setProgress', progress)

const requestFunc = config => setProgress(config, 50) && config
axios.interceptors.request.use(requestFunc)

axios.get = (url, params, config) => axios({
  method: 'get',
  params,
  url,
  ...config
})

axios.post = (url, params, config) => axios({
  method: 'post',
  params,
  url,
  ...config
})

const responseFunc = response => setProgress(response.config, 100) && response

axios.interceptors.response.use(responseFunc, (response) => {
  alert(response.message)
  store.dispatch('setProgress', 100)
})

Vue.prototype.$http = axios
